package com.test.system.admin.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.test.system.admin.dao.PermDao;
import com.test.system.admin.shiro.MyShiroFilterFactoryBean;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import com.test.system.admin.pojo.Role;
import com.test.system.admin.dao.RoleDao;

@Service
public class RoleService {
    @Resource
    private RoleDao roleDao;
    @Autowired
    private PermDao permDao;

    public int insert(Role pojo) {
        return roleDao.insert(pojo);
    }

    public int update(Role pojo) {
        return roleDao.update(pojo);
    }

    public PageInfo getRoleList(Role role, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize, "id desc");
        return new PageInfo(roleDao.getRoleList(role));
    }

    public List<Role> getRoleList() {
        return roleDao.getRoleList(null);
    }
    @Autowired
    private MyShiroFilterFactoryBean filterFactoryBean;
    public void insertRoleMenu(int roleId, int[] menuIds) {
        permDao.deleteRoleMenu(roleId);
        for (int menuId : menuIds) {
            permDao.insertRoleMenu(roleId, menuId);
        }
        filterFactoryBean.update();//刷新权限列表
    }

    public List<Integer> getRoleMenuIds(int roleId) {
        return permDao.getRoleMenuIds(roleId);
    }
}
